import { reqGetDetailData } from "@/api"

const state = {
  // 详情数据
  detailData: {},
}

const actions = {
  // 获取详情页数据
  async getDetailData({ commit }, id) {
    const result = await reqGetDetailData(id)
    if (result.code === 200) {
      // console.log("detail--->", result.data)
      // console.log("detail----->", result.data.spuSaleAttrList)
      commit("DETAIL_DATA", result.data)
    } else {
      console.log("detail获取数据失败", result.message)
    }
  },
}

const mutations = {
  // 保存详情数据
  DETAIL_DATA(state, payload) {
    state.detailData = payload
  },
  // 更改放大镜图片的默认图片链接
  UPDATE_IMG_DEFAULT_URL(state, imgUrl) {
    state.detailData.skuInfo.skuDefaultImg = imgUrl
  },
}

const getters = {
  // 分类条
  categoryView(state) {
    return state.detailData.categoryView || {}
  },
  // 商品规格
  skuInfo(state) {
    return state.detailData.skuInfo || {}
  },
  // 商品种类
  spuSaleAttrList(state) {
    return state.detailData.spuSaleAttrList || []
  },
}

export default {
  namespaced: true, // 开启命名空间
  state,
  actions,
  mutations,
  getters,
}
